/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import java.sql.Date;

import java.util.Calendar;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import model.DAO.CuocGoiDAO;
import model.DAO.CustomerDAO;
import model.DAO.RoleCuocGoiDAO;
import model.DAO.RoomDAO;
import model.entities.CuocGoi;
import model.entities.Customer;
import model.entities.RoleCuocGoi;
import model.entities.Room;

/**
 *
 * @author Hieu.Nguyen
 */
public class ThemQLCuocGoi extends javax.swing.JFrame {

    /**
     * Creates new form ThemQLCuocGoi
     */
    public ThemQLCuocGoi() {
        
        initComponents();
        loadCBORoleCG();
        loadCBOTenKH();
        loadCBOSoPhong();
       
    }
    public void loadCBORoleCG() {
            List<RoleCuocGoi> listRLG = RoleCuocGoiDAO.getInstance().getRoleCuocGoi(true);
            for (RoleCuocGoi ro : listRLG) {
            cboRoleCuocGoi.addItem(ro.getRoleCuocGoiID()+ "-" + ro.getRoleCuocGoiName());
        }
    }
    
    public void loadCBOTenKH() {
            List<Customer> listCustomer = CustomerDAO.getInstance().getCustomer(true);
            for (Customer ro : listCustomer) {
            cboTenKH.addItem(ro.getCustomerID()+ "-" + ro.getFirstName()+" "+ro.getMiddleName()+" "+ro.getLastName());
        }
    }
    
    public void loadCBOSoPhong() {
            List<Room> listRoom = RoomDAO.getInstance().getRoom(true);
            for (Room ro : listRoom) {
            cboSoPhong.addItem(ro.getRoomID() +"-"+ ro.getRoomNumber());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        btnAddCuocGoi = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        dtNgaytKTG = new com.toedter.calendar.JDateChooser();
        cboRoleCuocGoi = new javax.swing.JComboBox();
        cboTenKH = new javax.swing.JComboBox();
        cboSoPhong = new javax.swing.JComboBox();
        dtNgayBDGoi = new com.toedter.calendar.JDateChooser();
        jScrollPane2 = new javax.swing.JScrollPane();
        txaNDCG = new javax.swing.JTextArea();
        java.util.Date date1 = new java.util.Date();
        SpinnerDateModel sm1 =
        new SpinnerDateModel(date1, null, null, Calendar.HOUR_OF_DAY);
        spnTimeBD = new javax.swing.JSpinner(sm1);
        java.util.Date date = new java.util.Date();
        SpinnerDateModel sm =
        new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY);
        spnTimeKT = new javax.swing.JSpinner(sm);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 255, 102));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel1.setText("Them Quan Ly Cuoc Goi");

        jLabel2.setText("RoleCuocGoi         :");

        jLabel3.setText("TenKhachHang     :");

        jLabel4.setText("SoPhong               :");

        jLabel5.setText("TimeBD");

        jLabel9.setText("NoiDungCuocGoi   :");

        jLabel10.setText("NgayBDGoi           :");

        jButton1.setText("Nhap Lai");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Thoat");

        btnAddCuocGoi.setText("Them");
        btnAddCuocGoi.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAddCuocGoiMouseClicked(evt);
            }
        });

        jLabel12.setText("NgayKTGoi           :");

        jLabel13.setText("TimeKT");

        cboRoleCuocGoi.setMaximumRowCount(80);

        cboTenKH.setMaximumRowCount(80);

        cboSoPhong.setMaximumRowCount(80);

        txaNDCG.setColumns(20);
        txaNDCG.setRows(5);
        jScrollPane2.setViewportView(txaNDCG);

        JSpinner.DateEditor de = new JSpinner.DateEditor(spnTimeBD, "HH:MM:ss");
        spnTimeBD.setEditor(de);

        JSpinner.DateEditor de1 = new JSpinner.DateEditor(spnTimeKT, "HH:MM:ss");
        spnTimeKT.setEditor(de1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(321, 321, 321)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnAddCuocGoi, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                                    .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(cboRoleCuocGoi, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cboTenKH, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cboSoPhong, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(dtNgayBDGoi, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                                    .addComponent(spnTimeBD))))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(61, 61, 61)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(dtNgaytKTG, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                                    .addComponent(spnTimeKT)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jButton1)
                                .addGap(18, 18, 18)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel12)
                        .addComponent(cboRoleCuocGoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(dtNgaytKTG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel13)
                    .addComponent(cboTenKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spnTimeKT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cboSoPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(dtNgayBDGoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addGap(23, 23, 23)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(spnTimeBD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnAddCuocGoi)
                            .addComponent(jButton1)
                            .addComponent(jButton2))
                        .addGap(105, 105, 105))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnAddCuocGoiMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddCuocGoiMouseClicked
        // TODO add your handling code here:

        String roleCuocGoi = cboRoleCuocGoi.getSelectedItem().toString();
        String[] arr1 = roleCuocGoi.split("-");
        int idRoleCG = Integer.parseInt(arr1[0]);        
        RoleCuocGoi rcm = RoleCuocGoiDAO.getInstance().getRoleCuocGoiByID(idRoleCG);
        
        String customer = cboTenKH.getSelectedItem().toString();
        String[] arr2 = customer.split("-");
        int idcustomer = Integer.parseInt(arr2[0]);        
        Customer cmt = CustomerDAO.getInstance().getCustomerByID(idcustomer);
        
        String roomNumber = cboSoPhong.getSelectedItem().toString();
        String[] arr3 = roomNumber.split("-");
        int idroomNumber = Integer.parseInt(arr3[0]);        
        Room rm = RoomDAO.getInstance().getRoomByID(idroomNumber);
        
        java.util.Date utilDate = dtNgayBDGoi.getDate();
        java.sql.Date cceDateBD = new java.sql.Date(utilDate.getTime());
        
        String strDateFormat24a = "HH:mm:ss";
        
        String strDateFormat24b = "HH:mm:ss";
        
        JSpinner.DateEditor de = new JSpinner.DateEditor(spnTimeBD, strDateFormat24a);
        String TimeBD= de.getFormat().format(spnTimeBD.getValue());
        
        
        
        java.util.Date utilDate1 = dtNgaytKTG.getDate();
        java.sql.Date cceDateKT = new java.sql.Date(utilDate1.getTime());
        
        
        JSpinner.DateEditor dd = new JSpinner.DateEditor(spnTimeKT, strDateFormat24b);
        String timeKT= de.getFormat().format(spnTimeKT.getValue());
        
        String noiDungCG = txaNDCG.getText();
        

        CuocGoi cuocgoi;
        cuocgoi = new CuocGoi(rm, rcm, cmt, cceDateBD, TimeBD, cceDateKT, timeKT, noiDungCG);

        if(CuocGoiDAO.getInstance().createCuocGoi(cuocgoi)) {
                JOptionPane.showMessageDialog(null,  "Success");
        }else{
            JOptionPane.showMessageDialog(null,  "false");
        }
    }//GEN-LAST:event_btnAddCuocGoiMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ThemQLCuocGoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ThemQLCuocGoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ThemQLCuocGoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThemQLCuocGoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ThemQLCuocGoi().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddCuocGoi;
    private javax.swing.JComboBox cboRoleCuocGoi;
    private javax.swing.JComboBox cboSoPhong;
    private javax.swing.JComboBox cboTenKH;
    private com.toedter.calendar.JDateChooser dtNgayBDGoi;
    private com.toedter.calendar.JDateChooser dtNgaytKTG;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner spnTimeBD;
    private javax.swing.JSpinner spnTimeKT;
    private javax.swing.JTextArea txaNDCG;
    // End of variables declaration//GEN-END:variables
}
